import argparse

from align.text_from_html import get_text
from similarity import SimilarityCount
from utils import split_enzh


class AlignerCount(object):
    def __init__(self):
        self.sim = SimilarityCount("../ecdict.gb.txt")
        self.threshold = 0.22

    def align(self, en_p_list, zh_p_list):
        alignment = []
        for en in en_p_list:
            max_s = 0.0
            max_zh = ""
            for zh in zh_p_list:
                score = self.sim.score(en, zh)
                if score > max_s:
                    max_s = score
                    max_zh = zh
            alignment.append((en, max_zh, max_s))

        alignment = list(filter(lambda a: a[2]>self.threshold, alignment))
        return alignment


if __name__ == "__main__":
    argparser = argparse.ArgumentParser()
    argparser.add_argument("html_file")
    args = argparser.parse_args()

    p_list = get_text(args.html_file)
    print(p_list)

    en_p, zh_p = split_enzh(p_list)
    print(en_p)
    print(zh_p)

    aligner = AlignerCount()
    alignment = aligner.align(en_p, zh_p)
    for e, z, s in alignment:
        print(e + "\n" + z)
        print(s)
        print()
